﻿using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;
using Ricciolo.PaperBoy.Feeds;
using Project_CFT2.Behavior;
using Project_CFT2.ViewModel;
using Project_CFT2.View;
using Project_CFT2.Factory;
using Project_CFT2.RFID;
using TableCamCommunicator;
using GroupLab.Networking;
using Multitouch.Framework.WPF.Input;
using System.Windows.Controls;
using System.Threading;
using System;
using System.Windows.Media.Imaging;


namespace Project_CFT2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public delegate void showPopup(object x, string furnitureName);
    
    public partial class MainWindow
    {
        rfid rfid = new rfid();
        SharedDictionary SD;
        Subscription subscription1;
        //public static List<Project_CFT2.View.RoomView> Rooms;

        public MainWindow()
        {

            Multitouch.Framework.WPF.Input.MultitouchScreen.AllowNonContactEvents = true;
            InitializeComponent();
         //   this.Width = 1280;
            //this.Height = 1024;
           // this.Top = 10;
            
            
            
            rfid.start(this);

            SD = new SharedDictionary();
            subscription1 = new Subscription();
            subscription1.Dictionary = SD;

            SD.Url = "tcp://localhost:shareD";
            SD.Connected+= new System.EventHandler(sd_Connected);
            SD.Open();
                        
            /*DataClasses1DataContext _dataDC = new DataClasses1DataContext();
            ObservableFurnitureItems _FurnitureItems;
            _FurnitureItems = new ObservableFurnitureItems(_dataDC);
            // Initialize the f orm by creating the root folder view, using the mainPanel's ViewFactory
            //PanelBehavior.GetViewFactory(mainPanel).CreateViewFromDataContext(new FolderViewModel(FeedsManager.Current.RootFolder), new Point(100, 100));
            Furniture x;
            //x=_FurnitureItems;
            
            foreach (var item1 in _FurnitureItems)
            {
                
                x = item1;
                PanelBehavior.GetViewFactory(mainPanel).CreateViewFromDataContext(new FolderViewModel(x), new Point(100, 100));
            }
            */
            //PanelBehavior.GetViewFactory(mainPanel).CreateViewFromDataContext(new FolderViewModel(x), new Point(100, 100));
           
            //PanelBehavior.GetViewFactory(mainPanel).CreateViewFromDataContext(new FolderViewModel(FeedsManager.Current.RootFolder), new Point(100, 100));


            FrameworkElement newView3;
            newView3 = new FurniturePopupView();

            //newView3.SetValue(Canvas.LeftProperty, this.Width - 320);
            //newView3.SetValue(Canvas.TopProperty, this.Height/2 - 500/2-100);

            //newView3.SetValue(Canvas.LeftProperty, 965.0);
            //newView3.SetValue(Canvas.TopProperty, 100.0);

            newView3.SetValue(Canvas.LeftProperty, 10.0);
            newView3.SetValue(Canvas.TopProperty, 100.0);
            mainPanel2.Children.Add(newView3);

            FrameworkElement newView4;
            newView4 = new FurniturePopupView2();

            newView4.SetValue(Canvas.LeftProperty, 10.0);
            newView4.SetValue(Canvas.TopProperty, 100.0);
            mainPanel2.Children.Add(newView4);

            
            int scrollviewPositionX = (int) (this.Width/2 - 800/2);
            int scrollviewPositionY = (int)(this.Height - 250);
            
            PanelBehavior.GetViewFactory(mainPanel2).CreateViewFromDataContext(new FolderViewModel(), new Point(200, 750));
            PanelBehavior.GetViewFactory(mainPanel2).CreateViewFromDataContext(new FolderViewModel2(), new Point(1000, 200));
            PanelBehavior.GetViewFactory(mainPanel2).CreateViewFromDataContext(new RoomViewModel(), new Point(400, 10));
            
            
            
            
            
            //PanelBehavior.GetViewFactory(mainPanel2).CreateViewFromDataContext(new FolderViewModel(FeedsManager.Current.RootFolder), new Point(500, 0));
          //  PanelBehavior.GetViewFactory(mainPanel2).CreateViewFromDataContext(new RoomViewModel(), new Point(100, 0));
            
            
            
            //PanelBehavior.GetViewFactory(mainPanel3).CreateViewFromDataContext(new FolderViewModel(FeedsManager.Current.RootFolder), new Point(100, 100));
            //furnitureFactory x = new furnitureFactory();
            //x.CreateViewFromDataContext(new FolderViewModel(FeedsManager.Current.RootFolder), new Point(100, 100));
           // Rooms = new List<Project_CFT2.View.RoomView>();
        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            rfid.stop();
        }

        private void sd_Connected(object sender, System.EventArgs e)
        {
            this.subscription1.BeginInit();
            this.subscription1.Pattern = "/pointOne";//set the pattern to subscribe to
            this.subscription1.Notified += new SubscriptionEventHandler(subscription1_Notified);
            this.subscription1.EndInit();
            TouchInfo temp = new TouchInfo();
            temp.X = 23;

            SD["/pointone"] = temp;
            
            //this.btnConnect.Enabled = false;
            // disable the connect button
        }


        private void subscription1_Notified(object sender, GroupLab.Networking.SubscriptionEventArgs e)
        {
            //this.listBox1.Items.Clear();// clear the list

            // get the vector from the shared dictionary

            TouchInfo t = (TouchInfo) this.SD.GetEntry("/pointOne").Value;


            //txtYCoord.Text = this.SD.GetEntry("/shareD1/ycoord").Value.ToString();

            //SharedDictionary.Vector v = sd["/shareD1"] as SharedDictionary.Vector;

            // repopulate the list
            //for (int i = 0; i < v.Count; i++)
            // this.listBox1.Items.Add(v[i]);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
//            showFurnitureAddedPopup(sender);
        }

        public void addRFIDFurniture(Furniture item)
        {
            myFurnitureDataItem x1 = new myFurnitureDataItem(item);
            
            //assume only 1 room
            foreach (Project_CFT2.View.RoomView room in DropFurnitureBehaviour.Rooms)
            {
                room.AddItem(new FurnitureViewModel(x1), new Point(500.0, 500.0), mainPanel2 as Panel);
            }
        }

        public static void showFurnitureAddedPopup(object x, string furnitureName){

            var y = x as MainWindow;
            
            
            var panel = (Panel)y.mainPanel2;
            
            
            //FurnitureAddedPopup.Visibility = Visibility.Visible;


            Image img;

            foreach (var item1 in FolderViewModel._furnitureItems)
            {
                
                if (item1.name == "HOPEN" && furnitureName == "HOPEN")
                {

                    

                    y.FurnitureAddedPopup.Visibility = Visibility.Hidden;
                    y.addRFIDFurniture(item1);
                    y.FurnitureAddedPopup.furnitureAddedImage.Source = new BitmapImage(new Uri(item1.im_src.ToString(), UriKind.Relative));
                    y.FurnitureAddedPopup.Visibility = Visibility.Visible;
                    
                }
                else if (item1.name == "NORRSTEN" && furnitureName == "NORRSTEN")
                {
                    y.FurnitureAddedPopup.Visibility = Visibility.Hidden;
                    y.addRFIDFurniture(item1);

                    y.FurnitureAddedPopup.furnitureAddedImage.Source = new BitmapImage(new Uri(item1.im_src.ToString(), UriKind.Relative));
                    y.FurnitureAddedPopup.Visibility = Visibility.Visible;
                }
                else if (item1.name == "STOCKHOLM" && furnitureName == "STOCKHOLM")
                {
                    y.FurnitureAddedPopup.Visibility = Visibility.Hidden;
                    y.addRFIDFurniture(item1);
                    y.FurnitureAddedPopup.furnitureAddedImage.Source = new BitmapImage(new Uri(item1.im_src.ToString(), UriKind.Relative));
                    y.FurnitureAddedPopup.Visibility = Visibility.Visible;
                }
            }

        }

       
       
    }
}
